// pages/dynamic/dynamicHome/dynamicHome.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_request: 1, // 开关操作
    dynamic: [], // 服务动态数据
    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,
    changeload: false, // 数据加载状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    _PAGE = 1;
    that.getDynamicList(); // 通知消息
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    _PAGE = 1;
    that.getDynamicList(); // 通知消息
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var hasMore = that.data.hasMore; // 开关值
    if (!hasMore) {
      return false;
    }
    _PAGE++;
    that.getDynamicList(); // 通知消息
  },

  // 获取服务动态列表
  getDynamicList() {
    var that = this;
    that.setData({
      changeload: true
    })
    app.ajax({
      url: 'Common/Dynamic/dynamicList',
      data: {
        'type': 2,
        page: _PAGE,
        pageSize: _PAGESIZE
      },
      success: function(res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            //console.log('操作', res.data.data[0].dynamicData[0].image)
            that.setData({
              dynamic: res.data.data,
              empty: false
            })
          } else {
           
            that.setData({
              dynamic: that.data.dynamic.concat(res.data.data),
              empty: false
            })
            
          }


          // 是否加载更多
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load=false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
          // app.showToast(res.data.message);
        }
        that.setData({
          loadShow: true,
          changeload: false
        })
        
      }
    })
  },

  // 服务动态详情点击
  clickDetail: function(e) {
    console.log('动态ID', e.currentTarget.dataset.id);
    var that = this;
    var dynamic_id = e.currentTarget.dataset.id; // 获取动态ID
    wx.navigateTo({
      url: "../../dynamic/dynamicDetail/dynamicDetail?id=" + dynamic_id
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    var that = this;
    that.setData({
      loadShow: false
    })


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.removeStorageSync('dynamicIndex');
  },

  //图片加载错误
  dynamic_error(e) {
    var that = this,
      dynamic = that.data.dynamic,
      inds = e.currentTarget.dataset.index;
    dynamic[0].dynamicData[0].image = '../../../images/none.png'
    that.setData({
      dynamic: dynamic
    })
  },
  //图片加载错误
  images_error(e) {
    var that = this,
      dynamic = that.data.dynamic,
      inds = e.currentTarget.dataset.index,
      nowIndex = e.currentTarget.dataset.nowindex;
    dynamic[inds].dynamicData[nowIndex].image = '../../../images/none.png'
    that.setData({
      dynamic: dynamic
    })
  },
})